// pages/profile/profile.js
var app = getApp();
const fetch = require('../../utils/fetch') 
import Cache from "../../utils/cache";
import Tool from "../../utils/tool";


Page({
  /**
   * 页面的初始数据
   */
  data: {
    ishowtcDialog:false,//是否显示提示对话框
    isShowInfo:true,
    userInfo: {
      isLogin: false,
      username: "还未登录，请先登录！",
      usertype: undefined,
      starttime: undefined,
      endtime: undefined,
      headpic: undefined,
      wxid: undefined
    },
    serverpic:[
    // 'https://hao1.qhimg.com/t01a4719eed5998ac4b.png' //客服二维码图片
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!Tool.isEmpty(app.systemInfo.serverpic)) { //客服图片地址不为空 
      var dd=[];
      dd[0]= app.config.apiImgBase+app.systemInfo.serverpic;
      this.setData({
        serverpic: dd
      });
    }else{
      //从缓存中取
      const SERVERPIC = Cache.getCache(app.globalData.SERVERPIC); 
      if(!Tool.isEmpty(SERVERPIC)){
        app.systemInfo.serverpic=app.config.apiImgBase+app.systemInfo.serverpic;
        var dd=[];
        dd[0]= app.systemInfo.serverpic;
        this.setData({
          serverpic: dd
        });
      }
    }
    this.initLoginState();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updateDataInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  
  updateDataInfo: function () {
    this.setData({
      'userInfo.isLogin': app.userInfo.isLogin,
      'userInfo.username': app.userInfo.username,
      'userInfo.usertype': app.userInfo.usertype,
      'userInfo.starttime': app.userInfo.starttime,
      'userInfo.endtime': app.userInfo.endtime,
      'userInfo.headpic': app.userInfo.headpic, 
      'userInfo.wxid': app.userInfo.wxid
    }); 
  },
  // 验证登录状态
  initLoginState: async function () {
    if (app.userInfo.isLogin) { //已经登录

    } else { //去缓存中获取 登录信息 并赋值给全局变量
      const WXID = Cache.getCache(app.globalData.WXID);
      const USERTYPE = Cache.getCache(app.globalData.USERTYPE);
      const STARTTIME = Cache.getCache(app.globalData.STARTTIME);
      const HEADPIC = Cache.getCache(app.globalData.HEADPIC); 
      const ENDTIME = Cache.getCache(app.globalData.ENDTIME);
      const USERNAME = Cache.getCache(app.globalData.USERNAME);
      if (!Tool.isEmpty(WXID)) {
        //登录状态 则进行赋值 
        app.userInfo.isLogin = true;
        app.userInfo.wxid = WXID;
        app.userInfo.usertype = USERTYPE;
        app.userInfo.starttime = STARTTIME;
        app.userInfo.endtime = ENDTIME;
        app.userInfo.username = USERNAME;
        app.userInfo.headpic = HEADPIC;
       
      }
    }
  },
  // 登录操作
  getLoginUser: function () {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  createCardId: function () {
  
    //弹框  
    wx.showModal({
      content: '选择生成卡密类型',
      cancelText: '年卡',
      confirmText: '终身会员',
      success(res) {
        if (res.cancel || res.confirm) {
          var cartype;
          if (res.cancel) {
            // 年卡
            cartype = '2'; //年卡
          } else if (res.confirm) {
            //终身卡
            cartype = '3'; //终身会员
          }
          var data = {
            cartype: cartype,
            wxid: app.userInfo.wxid
          };
          fetch(`/creatCartNo`, data, "post")
            .then(response => {
              console.log(response.data.code === 200);
              if (response.data.code === 200) {
            
                var showdata = response.data.data.cardNo;
                //谈对话框   复制粘贴
                wx.showModal({
                  content: showdata,
                  confirmText: "复制",
                  showCancel: false,
                  success: function (res) {
                    wx.setClipboardData({
                      data: showdata,
                      success: function (res) {}
                    })
                  }
                })
              } else { 
                wx.showToast({
                  icon: "error",
                  title: response.data.msg,
                  duration: 1000
                });
              }
            }).catch(response => {
              wx.showToast({
                icon: "error",
                title: "生成卡密失败"
              });
            })
        }

      }
    })
  },
  cardInto: function () {
    //跳转至私密列表
    wx.switchTab({
      url: '/pages/card/card'
    })
  },
 
  copySimiList: function () {
    //跳转至私密列表
    wx.navigateTo({
      url: '/pages/catitem/catitem?id=11&name=私密资源'
    }) 
  },
  // 退出登录  清空全局变量的user对象
  logout: async function () {
    this.setData({ishowtcDialog:true}) 
  },  
  //搭建同款小程序
  showServer:   function () {
    let _this = this
    console.log(_this.data.serverpic)
    　　//图片预览
        wx.previewImage({
          current:_this.data.serverpic[0] , // 当前显示图片的http链接
          urls: _this.data.serverpic // 需要预览的图片http链接列表
        })
  },  
  tcComplete: function (e) {  
    this.setData({ishowtcDialog:false})
    if(e.detail.confirm){
      //退出登录
      app.userInfo.isLogin = false;
      app.userInfo.wxid = null;
      app.userInfo.usertype = null;
      app.userInfo.starttime = null;
      app.userInfo.endtime = null;
      app.userInfo.headpic = null; 
      app.userInfo.username = "还未登录，请先登录！";
      Cache.removeCache(app.globalData.WXID);
      Cache.removeCache(app.globalData.USERTYPE);
      Cache.removeCache(app.globalData.STARTTIME);
      Cache.removeCache(app.globalData.ENDTIME);
      Cache.removeCache(app.globalData.USERNAME);
      Cache.removeCache(app.globalData.HEADPIC);
      this.updateDataInfo();
      
    }
  } 

})